package com.zmn.oms.dubbo.impl.conf.redistribute;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.conf.distributereason.ConfOrderDistributeReasonBService;
import com.zmn.oms.common.dio.conf.redistribute.RedistributeConfQuery;
import com.zmn.oms.common.dro.conf.redistribute.ConfRedistributeStepDRO;
import com.zmn.oms.common.dro.conf.redistribute.RedistributeReasonConfDRO;
import com.zmn.oms.common.dro.conf.redistribute.RedistributeReasonSimpleConfDRO;
import com.zmn.oms.dubbo.interfaces.conf.redistribute.RedistributeConfListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.bo.conf.distribute.reason.ConfOrderDistributeReasonBO;
import com.zmn.oms.model.entity.conf.distributereason.ConfOrderDistributeReasonQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author sunlife
 * @date:2022/5/10 下午5:28
 * description: 订单改派配置查询
 */
@Slf4j
@DubboService(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION)
public class RedistributeConfListRemoteServiceImpl implements RedistributeConfListRemoteService {

    @Autowired
    ConfOrderDistributeReasonBService confOrderDistributeReasonBService;

    @Override
    public ResponseDTO<List<RedistributeReasonSimpleConfDRO>> listAllConf(RedistributeConfQuery query) {

        ConfOrderDistributeReasonQuery reasonQuery = BeanMapper.map(query, ConfOrderDistributeReasonQuery.class);
        reasonQuery.setOrderType(String.format("%s", query.getType()));
        reasonQuery.setApplyType(String.format("%s", query.getApplyType()));
        reasonQuery.setStatus(query.getStatus());
        List<RedistributeReasonSimpleConfDRO> confList = confOrderDistributeReasonBService.listSimpleConfByQuery(reasonQuery);

        return ResponseDTO.success(confList);
    }

    @Override
    public ResponseDTO<RedistributeReasonConfDRO> getById(@Valid @NotNull(message = "改派缘由ID不能为空") Integer reasonId) {

        ConfOrderDistributeReasonBO reasonBO = confOrderDistributeReasonBService.getConfOrderDistributeReasonById(reasonId);
        if (Objects.isNull(reasonBO)) {
            return ResponseDTO.success();
        }
        RedistributeReasonConfDRO confDRO = new RedistributeReasonConfDRO();

        confDRO.setId(reasonBO.getId());
        confDRO.setReason(reasonBO.getReason());
        confDRO.setLiableType(reasonBO.getLiableType());
        confDRO.setEngineerRedistributeStep(BeanMapper.map(reasonBO.getEngineerRedistributeStep(), ConfRedistributeStepDRO.class));
        confDRO.setSystemRedistributeStep(BeanMapper.map(reasonBO.getSystemRedistributeStep(), ConfRedistributeStepDRO.class));
        confDRO.setCountEnable(reasonBO.getCountEnable());
        confDRO.setSort(reasonBO.getSort());
        confDRO.setStatus(reasonBO.getStatus());


        List<Integer> applyTypeList = Arrays.asList(reasonBO.getApplyType().split(",")).stream().map(Integer::valueOf).collect(Collectors.toList());
        confDRO.setApplyType(applyTypeList);
        List<Integer> orderTypeList = Arrays.asList(reasonBO.getOrderType().split(",")).stream().map(Integer::valueOf).collect(Collectors.toList());
        confDRO.setOrderType(orderTypeList);
        List<Integer> platList = Arrays.asList(reasonBO.getPlat().split(",")).stream().map(Integer::valueOf).collect(Collectors.toList());
        confDRO.setOrderType(platList);

        return ResponseDTO.success(confDRO);
    }
}
